package fragmentson;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.widget.NestedScrollView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import activity.SonActivity;
import bean.EntityForSimple;
import bean.MessageForSimple;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;
import util.cache.ImageLoaderForReal;
import util.dialog.DialogForPhoto;
import util.dialog.DialogFromMiddle;


/**
 * 设备详情
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class DeviceParticulars extends BackHandledFragment {

	private View rootView,ll_unhasimei,ll_hasimei1,ll_hasimei2;
    private NestedScrollView scrollview;
	private EditText et_edittext1,et_edittext2,et_edittext3,et_edittext4,et_edittext5,et_edittext6,et_edittext7;
	private TextView tv_textview1,tv_textview2,tv_textview3,tv_textview4,tv_textview5,tv_textview6,tv_textview7,tv_textview8,tv_textview9,tv_modify;
	//门店 品牌 型号 颜色 物业 民警 电动车厂商
	private View ll_linear1,ll_linear2,ll_linear3,ll_linear4,ll_linear5,ll_linear6,ll_linear7;
	private ImageView iv_add_photo1,iv_add_photo2,iv_add_photo3,iv_add_photo4,iv_add_photo5,iv_add_photo6,iv_add_photo7,iv_add_photo8,iv_add_photo9;
	private ImageView iv_delete_photo1,iv_delete_photo2,iv_delete_photo3,iv_delete_photo4,iv_delete_photo5,iv_delete_photo6,iv_delete_photo7,iv_delete_photo8,iv_delete_photo9;
	//照片
	private int SYS_INTENT_REQUEST = 0XFF01,CAMERA_INTENT_REQUEST = 0XFF02,IMAGE_CUT = 1;
	public String photoPath,filename="";
	private Uri photoUri,imageUri;
	private int nowPhotoIndex = 0;
	//店铺id  品牌id   物业id  民警id  厂商id
	private String shopid="",brandid="",propertyid="",policeid="",producerid="",deviceid="";
    private Bitmap bmp;
    private HashMap<String,String> map = new HashMap<>();
	private ImageLoaderForReal imageloader = new ImageLoaderForReal(getActivity());

    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_deviceparticulars, null);
        initMap();
		initView();
		initData();
		return rootView;
	}

	private void initMap(){
        for(int i=1;i<10;i++) {
            map.put(i+"", "");
        }
    }

	private void initData(){
		ApiUtil.getApiService().getDevice(DemoApplication.getToken(),DemoApplication.getDeviceid()).enqueue(new Callback<MessageForSimple>() {
			@Override
			public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
				try {
					MessageForSimple entity = response.body();
					EntityForSimple data = entity.getData();
					if(entity.getCode().equals("10008")){
						//如果想改变地图中心点
						if(data.getImei().isEmpty()){
							ll_unhasimei.setVisibility(View.VISIBLE);
							ll_hasimei1.setVisibility(View.GONE);
							ll_hasimei2.setVisibility(View.GONE);
						}else {
							ll_unhasimei.setVisibility(View.GONE);
							ll_hasimei1.setVisibility(View.VISIBLE);
							ll_hasimei2.setVisibility(View.VISIBLE);
						}
						et_edittext1.setText(data.getDevicename());
						et_edittext2.setText(data.getPlatenumber());
						tv_textview1.setText(data.getShopname());
						tv_textview2.setText(data.getBrandname());
						tv_textview3.setText(data.getModel());
						tv_textview4.setText(data.getColor());
						et_edittext3.setText(data.getVinnumber());
						et_edittext4.setText(data.getMotornumber());
						et_edittext5.setText(data.getDis());
						tv_textview5.setText(data.getPropertyname());
						tv_textview6.setText(data.getPolicename());
						tv_textview7.setText(data.getProducername());
						tv_textview8.setText(data.getImei());
						tv_textview8.setText(data.getEndtime());

						shopid = data.getShopid();
						brandid = data.getBrandid();
						propertyid = data.getPropertyid();
						policeid = data.getPoliceid();
						producerid = data.getProducerid();
						deviceid = data.getDeviceid();

						/*String[] picurls = data.getSign().split(",");
						for(int i=1;i<picurls.length+1;i++){
							map.put(i+"",picurls[i-1]);
						}*/
						map.put("1",data.getImg1());
						map.put("2",data.getImg2());
						map.put("3",data.getImg3());
						map.put("4",data.getImg4());
						map.put("5",data.getImg5());
						map.put("6",data.getImg6());
						map.put("7",data.getImg7());
						map.put("8",data.getImg8());
						map.put("9",data.getImg9());
						imageloader.DisplayImage(map.get("1"),iv_add_photo1);
						imageloader.DisplayImage(map.get("2"),iv_add_photo2);
						imageloader.DisplayImage(map.get("3"),iv_add_photo3);
						imageloader.DisplayImage(map.get("4"),iv_add_photo4);
						imageloader.DisplayImage(map.get("5"),iv_add_photo5);
						imageloader.DisplayImage(map.get("6"),iv_add_photo6);
						imageloader.DisplayImage(map.get("7"),iv_add_photo7);
						imageloader.DisplayImage(map.get("8"),iv_add_photo8);
						imageloader.DisplayImage(map.get("9"),iv_add_photo9);
					}else{
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			@Override
			public void onFailure(Call<MessageForSimple> call, Throwable t) {
				Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
			}
		});
	}

	private void modify(){
		String sign = "";
		for(int i=1;i<10;i++){
			if(i!=9) {
				sign = sign + map.get(i + "") + ",";
			}else{
				sign = sign + map.get(i + "");
			}
		}
		ApiUtil.getApiService().deviceSet(DemoApplication.getToken(),DemoApplication.getDeviceid(),et_edittext1.getText().toString(),et_edittext2.getText().toString(),shopid,brandid,tv_textview3.getText().toString(),tv_textview4.getText().toString(),et_edittext3.getText().toString(),et_edittext4.getText().toString(),et_edittext5.getText().toString(),sign,propertyid,policeid,producerid).enqueue(new Callback<MessageForSimple>() {
			@Override
			public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
				try {
					MessageForSimple entity = response.body();
					if(entity.getCode().equals("10008")){
						Toast.makeText(getActivity(),"车辆信息保存成功",Toast.LENGTH_LONG).show();
						tv_modify.setText(getResources().getString(R.string.device_particulars26));
						closeModyfy();
					}else{
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			@Override
			public void onFailure(Call<MessageForSimple> call, Throwable t) {
				Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.title_car_more);
		tv_modify = ((TextView) rootView.findViewById(R.id.tv_modify));
		tv_modify.setOnClickListener(listener);
		scrollview = ((NestedScrollView) rootView.findViewById(R.id.scrollview));
		et_edittext1 = ((EditText) rootView.findViewById(R.id.et_edittext1));
		et_edittext2 = ((EditText) rootView.findViewById(R.id.et_edittext2));
		et_edittext3 = ((EditText) rootView.findViewById(R.id.et_edittext3));
		et_edittext4 = ((EditText) rootView.findViewById(R.id.et_edittext4));
		et_edittext5 = ((EditText) rootView.findViewById(R.id.et_edittext5));
		et_edittext6 = ((EditText) rootView.findViewById(R.id.et_edittext6));
		et_edittext7 = ((EditText) rootView.findViewById(R.id.et_edittext7));
		tv_textview1 = ((TextView) rootView.findViewById(R.id.tv_textview1));
		tv_textview2 = ((TextView) rootView.findViewById(R.id.tv_textview2));
		tv_textview3 = ((TextView) rootView.findViewById(R.id.tv_textview3));
		tv_textview4 = ((TextView) rootView.findViewById(R.id.tv_textview4));
		tv_textview5 = ((TextView) rootView.findViewById(R.id.tv_textview5));
		tv_textview6 = ((TextView) rootView.findViewById(R.id.tv_textview6));
		tv_textview7 = ((TextView) rootView.findViewById(R.id.tv_textview7));
		tv_textview8 = ((TextView) rootView.findViewById(R.id.tv_textview8));
		tv_textview9 = ((TextView) rootView.findViewById(R.id.tv_textview9));
		ll_linear1 = rootView.findViewById(R.id.ll_linear1);
		ll_linear2 = rootView.findViewById(R.id.ll_linear2);
		ll_linear3 = rootView.findViewById(R.id.ll_linear3);
		ll_linear4 = rootView.findViewById(R.id.ll_linear4);
		ll_linear5 = rootView.findViewById(R.id.ll_linear5);
		ll_linear6 = rootView.findViewById(R.id.ll_linear6);
		ll_linear7 = rootView.findViewById(R.id.ll_linear7);
		ll_unhasimei = rootView.findViewById(R.id.ll_unhasimei);
		ll_hasimei1  = rootView.findViewById(R.id.ll_hasimei1);
		ll_hasimei2 = rootView.findViewById(R.id.ll_hasimei2);
		iv_add_photo1 = (ImageView)rootView.findViewById(R.id.iv_add_photo1);
		iv_add_photo2 = (ImageView)rootView.findViewById(R.id.iv_add_photo2);
		iv_add_photo3 = (ImageView)rootView.findViewById(R.id.iv_add_photo3);
		iv_add_photo4 = (ImageView)rootView.findViewById(R.id.iv_add_photo4);
		iv_add_photo5 = (ImageView)rootView.findViewById(R.id.iv_add_photo5);
		iv_add_photo6 = (ImageView)rootView.findViewById(R.id.iv_add_photo6);
		iv_add_photo7 = (ImageView)rootView.findViewById(R.id.iv_add_photo7);
		iv_add_photo8 = (ImageView)rootView.findViewById(R.id.iv_add_photo8);
		iv_add_photo9 = (ImageView)rootView.findViewById(R.id.iv_add_photo9);
		iv_delete_photo1 = (ImageView)rootView.findViewById(R.id.iv_delete_photo1);
		iv_delete_photo2 = (ImageView)rootView.findViewById(R.id.iv_delete_photo2);
		iv_delete_photo3 = (ImageView)rootView.findViewById(R.id.iv_delete_photo3);
		iv_delete_photo4 = (ImageView)rootView.findViewById(R.id.iv_delete_photo4);
		iv_delete_photo5 = (ImageView)rootView.findViewById(R.id.iv_delete_photo5);
		iv_delete_photo6 = (ImageView)rootView.findViewById(R.id.iv_delete_photo6);
		iv_delete_photo7 = (ImageView)rootView.findViewById(R.id.iv_delete_photo7);
		iv_delete_photo8 = (ImageView)rootView.findViewById(R.id.iv_delete_photo8);
		iv_delete_photo9 = (ImageView)rootView.findViewById(R.id.iv_delete_photo9);

		rootView.findViewById(R.id.tv_adddevice).setOnClickListener(listener);
		rootView.findViewById(R.id.tv_delete).setOnClickListener(listener);
		rootView.findViewById(R.id.iv_help).setOnClickListener(listener);
		closeModyfy();

	}

	private void closeModyfy(){
		et_edittext1.setEnabled(false);
		et_edittext2.setEnabled(false);
		et_edittext3.setEnabled(false);
		et_edittext4.setEnabled(false);
		et_edittext5.setEnabled(false);
		et_edittext6.setEnabled(false);
		et_edittext7.setEnabled(false);
		ll_linear1.setOnClickListener(null);
		ll_linear2.setOnClickListener(null);
		ll_linear3.setOnClickListener(null);
		ll_linear4.setOnClickListener(null);
		ll_linear5.setOnClickListener(null);
		ll_linear6.setOnClickListener(null);
		ll_linear7.setOnClickListener(null);
		ll_hasimei1.setOnClickListener(null);

		iv_add_photo1.setOnClickListener(null);
		iv_add_photo2.setOnClickListener(null);
		iv_add_photo3.setOnClickListener(null);
		iv_add_photo4.setOnClickListener(null);
		iv_add_photo5.setOnClickListener(null);
		iv_add_photo6.setOnClickListener(null);
		iv_add_photo7.setOnClickListener(null);

		((ImageView) rootView.findViewById(R.id.iv_imageright1)).setVisibility(View.GONE);
		((ImageView) rootView.findViewById(R.id.iv_imageright2)).setVisibility(View.GONE);
		((ImageView) rootView.findViewById(R.id.iv_imageright3)).setVisibility(View.GONE);
		((ImageView) rootView.findViewById(R.id.iv_imageright4)).setVisibility(View.GONE);
		((ImageView) rootView.findViewById(R.id.iv_imageright5)).setVisibility(View.GONE);
		((ImageView) rootView.findViewById(R.id.iv_imageright6)).setVisibility(View.GONE);
		((ImageView) rootView.findViewById(R.id.iv_imageright7)).setVisibility(View.GONE);
		((ImageView) rootView.findViewById(R.id.iv_imageright8)).setVisibility(View.GONE);

        iv_delete_photo1.setVisibility(View.GONE);
        iv_delete_photo2.setVisibility(View.GONE);
        iv_delete_photo3.setVisibility(View.GONE);
        iv_delete_photo4.setVisibility(View.GONE);
        iv_delete_photo5.setVisibility(View.GONE);
        iv_delete_photo6.setVisibility(View.GONE);
        iv_delete_photo7.setVisibility(View.GONE);
        iv_delete_photo8.setVisibility(View.GONE);
        iv_delete_photo9.setVisibility(View.GONE);

	}

	private void openModyfy(){
		et_edittext1.setEnabled(true);
		et_edittext2.setEnabled(true);
		et_edittext3.setEnabled(true);
		et_edittext4.setEnabled(true);
		et_edittext5.setEnabled(true);
		et_edittext6.setEnabled(true);
		et_edittext7.setEnabled(true);
		ll_linear1.setOnClickListener(listener);
		ll_linear2.setOnClickListener(listener);
		ll_linear3.setOnClickListener(listener);
		ll_linear4.setOnClickListener(listener);
		ll_linear5.setOnClickListener(listener);
		ll_linear6.setOnClickListener(listener);
		ll_linear7.setOnClickListener(listener);
		ll_hasimei1.setOnClickListener(listener);

		iv_add_photo1.setOnClickListener(listener);
		iv_add_photo2.setOnClickListener(listener);
		iv_add_photo3.setOnClickListener(listener);
		iv_add_photo4.setOnClickListener(listener);
		iv_add_photo5.setOnClickListener(listener);
		iv_add_photo6.setOnClickListener(listener);
		iv_add_photo7.setOnClickListener(listener);
		iv_add_photo8.setOnClickListener(listener);
		iv_add_photo9.setOnClickListener(listener);

		((ImageView) rootView.findViewById(R.id.iv_imageright1)).setVisibility(View.VISIBLE);
		((ImageView) rootView.findViewById(R.id.iv_imageright2)).setVisibility(View.VISIBLE);
		((ImageView) rootView.findViewById(R.id.iv_imageright3)).setVisibility(View.VISIBLE);
		((ImageView) rootView.findViewById(R.id.iv_imageright4)).setVisibility(View.VISIBLE);
		((ImageView) rootView.findViewById(R.id.iv_imageright5)).setVisibility(View.VISIBLE);
		((ImageView) rootView.findViewById(R.id.iv_imageright6)).setVisibility(View.VISIBLE);
		((ImageView) rootView.findViewById(R.id.iv_imageright7)).setVisibility(View.VISIBLE);
		((ImageView) rootView.findViewById(R.id.iv_imageright8)).setVisibility(View.VISIBLE);

		iv_delete_photo1.setOnClickListener(listener);
		iv_delete_photo2.setOnClickListener(listener);
		iv_delete_photo3.setOnClickListener(listener);
		iv_delete_photo4.setOnClickListener(listener);
		iv_delete_photo5.setOnClickListener(listener);
		iv_delete_photo6.setOnClickListener(listener);
		iv_delete_photo7.setOnClickListener(listener);
		iv_delete_photo8.setOnClickListener(listener);
		iv_delete_photo9.setOnClickListener(listener);

		if(!map.get("1").equals("")) {
			iv_delete_photo1.setVisibility(View.VISIBLE);
		}
		if(!map.get("2").equals("")) {
			iv_delete_photo2.setVisibility(View.VISIBLE);
		}
		if(!map.get("3").equals("")) {
			iv_delete_photo3.setVisibility(View.VISIBLE);
		}
		if(!map.get("4").equals("")) {
			iv_delete_photo4.setVisibility(View.VISIBLE);
		}
		if(!map.get("5").equals("")) {
			iv_delete_photo5.setVisibility(View.VISIBLE);
		}
		if(!map.get("6").equals("")) {
			iv_delete_photo6.setVisibility(View.VISIBLE);
		}
		if(!map.get("7").equals("")) {
			iv_delete_photo7.setVisibility(View.VISIBLE);
		}
		if(!map.get("8").equals("")) {
			iv_delete_photo8.setVisibility(View.VISIBLE);
		}
		if(!map.get("9").equals("")) {
			iv_delete_photo9.setVisibility(View.VISIBLE);
		}
	}


	public static DeviceParticulars newInstance() {
		DeviceParticulars fragment = new DeviceParticulars();
		return fragment;
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.tv_modify:
					if(((TextView) v).getText().equals(getResources().getString(R.string.device_particulars26))){
						tv_modify.setText(getResources().getString(R.string.device_particulars27));
						openModyfy();
						scrollToPosition(0,0);
					}else{
						modify();
					}
					break;
				case R.id.ll_linear1:
					((SonActivity) getActivity()).addFragment(DeviceProperty.newInstance(new DeviceProperty.CallBackListener() {
						@Override
						public void listener(String item,String id) {
							shopid = id;
							tv_textview1.setText(item);
						}
					},"1",""),true);
					break;
				case R.id.ll_linear2:
					((SonActivity) getActivity()).addFragment(DeviceProperty.newInstance(new DeviceProperty.CallBackListener() {
						@Override
						public void listener(String item,String id) {
							brandid = id;
							tv_textview2.setText(item);
						}
					},"2",""),true);
					break;
				case R.id.ll_linear3:
					if(brandid.isEmpty()){
						Toast.makeText(getActivity(),"请先选择品牌",Toast.LENGTH_LONG).show();
						return;
					}
					((SonActivity) getActivity()).addFragment(DeviceProperty.newInstance(new DeviceProperty.CallBackListener() {
						@Override
						public void listener(String item,String id) {
							tv_textview3.setText(item);
						}
					},"3",brandid),true);
					break;
				case R.id.ll_linear4:
					if(brandid.isEmpty()){
						Toast.makeText(getActivity(),"请先选择品牌",Toast.LENGTH_LONG).show();
						return;
					}
					((SonActivity) getActivity()).addFragment(DeviceProperty.newInstance(new DeviceProperty.CallBackListener() {
						@Override
						public void listener(String item,String id) {
							tv_textview4.setText(item);
						}
					},"4",brandid),true);
					break;
				case R.id.ll_linear5:
					((SonActivity) getActivity()).addFragment(DeviceProperty.newInstance(new DeviceProperty.CallBackListener() {
						@Override
						public void listener(String item,String id) {
							propertyid = id;
							tv_textview5.setText(item);
						}
					},"5",""),true);
					break;
				case R.id.ll_linear6:
					((SonActivity) getActivity()).addFragment(DeviceProperty.newInstance(new DeviceProperty.CallBackListener() {
						@Override
						public void listener(String item,String id) {
							policeid = id;
							tv_textview6.setText(item);
						}
					},"6",""),true);
					break;
				case R.id.ll_linear7:
					((SonActivity) getActivity()).addFragment(DeviceProperty.newInstance(new DeviceProperty.CallBackListener() {
						@Override
						public void listener(String item,String id) {
							producerid = id;
							tv_textview7.setText(item);
						}
					},"7",""),true);
					break;
				case R.id.tv_adddevice:
				case R.id.ll_hasimei1:
					((SonActivity) getActivity()).addFragment(AddDevice.newInstance(deviceid));
					break;
				case R.id.iv_add_photo1:
					nowPhotoIndex = 1;
					showDialog();
					break;
				case R.id.iv_add_photo2:
					nowPhotoIndex = 2;
					showDialog();
					break;
				case R.id.iv_add_photo3:
					nowPhotoIndex = 3;
					showDialog();
					break;
				case R.id.iv_add_photo4:
					nowPhotoIndex = 4;
					showDialog();
					break;
				case R.id.iv_add_photo5:
					nowPhotoIndex = 5;
					showDialog();
					break;
				case R.id.iv_add_photo6:
					nowPhotoIndex = 6;
					showDialog();
					break;
				case R.id.iv_add_photo7:
					nowPhotoIndex = 7;
					showDialog();
					break;
				case R.id.iv_add_photo8:
					nowPhotoIndex = 8;
					showDialog();
					break;
				case R.id.iv_add_photo9:
					nowPhotoIndex = 9;
					showDialog();
					break;
				case R.id.iv_delete_photo1:
					map.put("1","");
					iv_add_photo1.setImageResource(R.mipmap.icon_add_photo);
					v.setVisibility(View.GONE);
					break;
				case R.id.iv_delete_photo2:
					map.put("2","");
					iv_add_photo2.setImageResource(R.mipmap.icon_add_photo);
					v.setVisibility(View.GONE);
					break;
				case R.id.iv_delete_photo3:
					map.put("3","");
					iv_add_photo3.setImageResource(R.mipmap.icon_add_photo);
					v.setVisibility(View.GONE);
					break;
				case R.id.iv_delete_photo4:
					map.put("4","");
					iv_add_photo4.setImageResource(R.mipmap.icon_add_photo);
					v.setVisibility(View.GONE);
					break;
				case R.id.iv_delete_photo5:
					map.put("5","");
					iv_add_photo5.setImageResource(R.mipmap.icon_add_photo);
					v.setVisibility(View.GONE);
					break;
				case R.id.iv_delete_photo6:
					map.put("6","");
					iv_add_photo6.setImageResource(R.mipmap.icon_add_photo);
					v.setVisibility(View.GONE);
					break;
				case R.id.iv_delete_photo7:
					map.put("7","");
					iv_add_photo7.setImageResource(R.mipmap.icon_add_photo);
					v.setVisibility(View.GONE);
					break;
				case R.id.iv_delete_photo8:
					map.put("8","");
					iv_add_photo8.setImageResource(R.mipmap.icon_add_photo);
					v.setVisibility(View.GONE);
					break;
				case R.id.iv_delete_photo9:
					map.put("9","");
					iv_add_photo9.setImageResource(R.mipmap.icon_add_photo);
					v.setVisibility(View.GONE);
					break;
				case R.id.tv_delete:
					DialogFromMiddle dialogFormMiddle = new DialogFromMiddle(getActivity());
					dialogFormMiddle.setSureListener(new DialogFromMiddle.Sure() {
						@Override
						public void onSure() {

							ApiUtil.getApiService().deviceDel(DemoApplication.getToken(),DemoApplication.getDeviceid()).enqueue(new Callback<MessageForSimple>() {
								@Override
								public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
									try {
										MessageForSimple entity = response.body();
										if (entity.getCode().equals("10008")) {
											((SonActivity) getActivity()).backTask();
										} else {
											Toast.makeText(getActivity(), entity.getMsg(), Toast.LENGTH_LONG).show();
										}
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
								@Override
								public void onFailure(Call<MessageForSimple> call, Throwable t) {
									Toast.makeText(getActivity(), getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
								}
							});
						}
					});
					dialogFormMiddle.show(getResources().getString(R.string.dialog_title6), getResources().getString(R.string.dialog_content6), "", "", getResources().getString(R.string.dialog_yes6),getResources().getString(R.string.dialog_no6),true);
					break;
				case R.id.iv_help:
					DialogFromMiddle dialogFormMiddlehelp = new DialogFromMiddle(getActivity());
					dialogFormMiddlehelp.setSureListener(new DialogFromMiddle.Sure() {
						@Override
						public void onSure() {
						}
					});
					dialogFormMiddlehelp.show(getResources().getString(R.string.dialog_title7), "", "", "", getResources().getString(R.string.dialog_yes7),"",false);
					break;
				default:
					break;
			}
		}
	};

	/**
	 * 打开系统相册
	 */
	private void systemPhoto() {
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType("image/*");
		startActivityForResult(intent, SYS_INTENT_REQUEST);
	}

	/**
	 * 调用相机拍照
	 */
	private void cameraPhoto() {
		String sdStatus = Environment.getExternalStorageState();
		/* 检测sd是否可用 */
		if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
			Toast.makeText(getActivity(),"SD卡不可用",Toast.LENGTH_LONG).show();
			return;
		}
		//图片名称 时间命名
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = new Date(System.currentTimeMillis());
		filename = format.format(date);
		//创建File对象用于存储拍照的图片 SD卡根目录
		//File outputImage = new File(Environment.getExternalStorageDirectory(),test.jpg);
		//存储至DCIM文件夹
		File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
		File outputImage = new File(path,filename+".jpg");
		try {
			if(outputImage.exists()) {
				outputImage.delete();
			}
			outputImage.createNewFile();
		} catch(IOException e) {
			e.printStackTrace();
		}
		//将File对象转换为Uri并启动照相程序
		imageUri = Uri.fromFile(outputImage);
		Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); //照相
		intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //指定图片输出地址
		//拍完照startActivityForResult() 结果返回onActivityResult()函数
		startActivityForResult(intent, CAMERA_INTENT_REQUEST);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == SYS_INTENT_REQUEST
				&& resultCode == getActivity().RESULT_OK&&data !=null) {
			cameraCamera(data);
		} else if (requestCode == CAMERA_INTENT_REQUEST
				&& resultCode == getActivity().RESULT_OK) {
			cameraCamera();
		} else if (requestCode == IMAGE_CUT
				&& resultCode == getActivity().RESULT_OK) {
			bmp = data.getParcelableExtra("data");
			if(bmp==null){
				if(photoUri!=null){
					try {
						bmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(photoUri));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
            photoPath = photoUri.getEncodedPath();
            ApiUtil.getApiService().saveImg(DemoApplication.getToken(), ApiUtil.upload(photoPath)).enqueue(new Callback<MessageForSimple>() {
                @Override
                public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
                    try {
                        MessageForSimple entity = response.body();
                        if(entity.getCode().equals("10008")){
                            map.put(nowPhotoIndex+"",entity.getData().getPicurl());
                            setPhoto(bmp);
                        }else{
                            Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onFailure(Call<MessageForSimple> call, Throwable t) {
                    Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
                }
            });

		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 拍照后获取照片
	 */
	private void cameraCamera() {
		Uri uri = null;
		if(imageUri!=null){
			uri = imageUri;
		}
		Intent intent = getImageClipIntent(uri);
		//广播刷新相册
		Intent intentBc = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
		intentBc.setData(imageUri);
		getActivity().sendBroadcast(intentBc);
		startActivityForResult(intent, IMAGE_CUT);
	}

	/**
	 * @param data
	 * 相册选择后后获取照片
	 */
	private void cameraCamera(Intent data) {
		Uri uri = data.getData();
		// 相册中文件绝对路径
		Intent intent = getImageClipIntent(uri);
		startActivityForResult(intent, IMAGE_CUT);
	}

	/**
	 * 获取剪切后的图片
	 */
	public Intent getImageClipIntent(Uri uri) {
		//图片名称 时间命名
		File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
		File outputImage = new File(path,"1.jpg");
		try {
			if(outputImage.exists()) {
				outputImage.delete();
			}
			outputImage.createNewFile();
		} catch(IOException e) {
			e.printStackTrace();
		}
		//将File对象转换为Uri并启动照相程序
		photoUri = Uri.fromFile(outputImage);

		Intent intent = new Intent("com.android.camera.action.CROP", null);
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("scale", true);
		intent.putExtra("crop", true);
		intent.putExtra("aspectX", 1);// 裁剪框比例
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 200);// 输出图片大小
		intent.putExtra("outputY", 200);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
		intent.putExtra("return-data", false);
		return intent;
	}

	public void scrollToPosition(int x,int y) {

		ObjectAnimator xTranslate = ObjectAnimator.ofInt(scrollview, "scrollX", x);
		ObjectAnimator yTranslate = ObjectAnimator.ofInt(scrollview, "scrollY", y);

		AnimatorSet animators = new AnimatorSet();
		animators.setDuration(500L);
		animators.playTogether(xTranslate, yTranslate);
		animators.addListener(new Animator.AnimatorListener() {

			@Override
			public void onAnimationStart(Animator arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onAnimationRepeat(Animator arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animator arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationCancel(Animator arg0) {
				// TODO Auto-generated method stub

			}
		});
		animators.start();
	}

	private void setPhoto(Bitmap bmp){
		switch (nowPhotoIndex){
			case 1:
				iv_add_photo1.setImageBitmap(bmp);
				iv_delete_photo1.setVisibility(View.VISIBLE);
				break;
			case 2:
				iv_add_photo2.setImageBitmap(bmp);
				iv_delete_photo2.setVisibility(View.VISIBLE);
				break;
			case 3:
				iv_add_photo3.setImageBitmap(bmp);
				iv_delete_photo3.setVisibility(View.VISIBLE);
				break;
			case 4:
				iv_add_photo4.setImageBitmap(bmp);
				iv_delete_photo4.setVisibility(View.VISIBLE);
				break;
			case 5:
				iv_add_photo5.setImageBitmap(bmp);
				iv_delete_photo5.setVisibility(View.VISIBLE);
				break;
			case 6:
				iv_add_photo6.setImageBitmap(bmp);
				iv_delete_photo6.setVisibility(View.VISIBLE);
				break;
			case 7:
				iv_add_photo7.setImageBitmap(bmp);
				iv_delete_photo7.setVisibility(View.VISIBLE);
				break;
			case 8:
				iv_add_photo8.setImageBitmap(bmp);
				iv_delete_photo8.setVisibility(View.VISIBLE);
				break;
			case 9:
				iv_add_photo9.setImageBitmap(bmp);
				iv_delete_photo8.setVisibility(View.VISIBLE);
				break;
		}
	}

	private void showDialog(){
		DialogForPhoto dialog = new DialogForPhoto(getActivity());
		dialog.setSureListener(new DialogForPhoto.Sure() {
			@Override
			public void onSure(int item) {
				switch (item){
					case 1:
						cameraPhoto();
						break;
					case 2:
						systemPhoto();
						break;
					default:
						break;
				}
			}
		});
		dialog.show();
	}

	@Override
	public boolean onBackPressed() {
		return false;
	}
	
}
